package git.soulbgm.mapreduce.reduceJoin;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

/**
 * @author SoulBGM
 * @date 2021/12/1 23:02
 */
public class TableMapper extends Mapper<LongWritable, Text, Text, TableBean> {

    private String fileName;
    private Text outK = new Text();
    private TableBean outV = new TableBean();
    private final String TABLE_NAME_ORDER = "order";
    private final String TABLE_NAME_PD = "pd";

    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        FileSplit fileSplit = (FileSplit) context.getInputSplit();
        this.fileName = fileSplit.getPath().getName();
    }

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        String[] split = line.split("\\s+");
        if (fileName.contains("order")) {
            outK.set(split[1]);
            outV.setId(Long.parseLong(split[0]));
            outV.setPid(split[1]);
            outV.setAmount(Integer.parseInt(split[2]));
            outV.setPname("");
            outV.setTableName(TABLE_NAME_ORDER);
        } else {
            outK.set(split[0]);
            outV.setId(0L);
            outV.setPid(split[0]);
            outV.setAmount(0);
            outV.setPname(split[1]);
            outV.setTableName(TABLE_NAME_PD);
        }
        context.write(outK, outV);
    }
}
